* use the study eligible population for the table...
use "${PilotPublicData}/hgopy_followup_public.dta", clear

*lighten up dataset
keep fup_id view *_2w ipweight
*rename
rename *_2w *
*relabel 

label var val_childif  "Asked about desire for another child"
label var val_childwhen  "Asked about preferred timing for another child"
label var val_fpexp  "Asked about previous FP experience"
label var val_fppref  "Asked about preferred FP method"
label var val_fpinfo  "Given information about different FP methods"
label var val_fpsideeffects  "Told about side effects or problems with selected method"
label var val_fpwhatotodo   "Told how to manage side effects or problems with selected method"
label var val_fpswitch   "Told about possibility of switching to another method"
label var val_fpwarning   "Told about warning signs for the selected method"
label var val_privacysee  "Consulted where nobody could see them"
label var val_privacyhear "Consulted where nobody could hear them"

* Client satisfaction
foreach v of varlist satisf_fp satisf_consult likely_return {
    gen d_`v'=`v'>=4 & !missing(`v')
}
label var d_satisf_fp "Satisfied with FP services in general" 
label var d_satisf_consult "Satisfied with FP consultation" 
label var d_likely_return "Likely to return for FP services" 
 
*quality of care figure
  *preserve
    count if view == 0
    local n_mim = `r(N)'
    count if view == 1
    local n_idm = `r(N)'
    count if view == 2
    local n_sdm = `r(N)'

    *just make a balance table 
    iebaltab 	index ///
				index_method val_childif val_childwhen val_fpexp val_fppref val_fpinfo ///
				index_use val_fpsideeffects val_fpwhatotodo val_fpwarning ///
				index_care val_fpswitch  ///
				index_respect val_privacysee val_privacyhear /// 
				d_satisf_fp d_satisf_consult d_likely_return ///
      [pw=ipweight] , grpvar(view) control(1) order(1 2)   /// if view!=0
      browse rowvarlabels tblnonote format(%9.2f) pt std starsnoadd 
	  
    *for now do by hand 
    drop if v1==""
    drop v2 v4 v6 v9
	*MiM first 
	order v7, after(v1)
    *set obs
    gen n=_n
	 
    set obs `=_N+5'
    replace n = 2.2 if _n == 21
    replace n = 2.3 if _n == 22
    replace n = 17.2 if _n == 23
    replace n = 17.3 if _n == 24
    replace n = _N  if _n == _N
    sort n
    drop n
 
    *sub headers
    replace v1 = "Sub-indices and index items" if _n == 4
    replace v1 = "Client Satisfaction"     if _n == 21
    replace v1 = "Audio and Visual Privacy" if v1 == "Respectful care"
    replace v1 = "Obs."                    if _n == _N
    replace v7 = "`n_mim'"                 if _n == _N
    replace v3 = "`n_idm'"                 if _n == _N 
    replace v5 = "`n_sdm'"                 if _n == _N 
	
	*keep only some p.values 
	replace v8=""         if !inlist(_n,2,5,11,15,17,22,23,24)
//   replace v1="~~" + v1  if !inlist(_n,1,2,3,4,5,11,15,17,20,21,25)
	
	*clean titles 
	foreach v of varlist v7 v3 v5 {
		replace `v' = "Mean" in 1
	}
	replace v8 = "(2)-(3)" in 1
    
	*save 
    #delimit ;
    run "${Programs}/texsave_custom.do" ; 
    texsave_custom using "${OutTexTab}/TabS8-quality-of-care-fullfupsample.tex", 
      replace nonames  hlines(1 20 25)  frag
      align("lcccc")
      headerlines(" & (1) & (2) & (3) & (4) " 
            " & \textbf{MiM} & \textbf{IDM} & \textbf{SDM} & Diff (p-value) ") 	;
    #delimit cr
